package com.work.keep.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.work.keep.R;
import com.work.keep.base.BaseFragment;
import com.work.keep.datas.PeopleData;
import com.work.keep.datas.PeopleRecordFragmentData;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;


/**
 * GET https://api.gotokeep.com/v1.1/people/56999b2d9951948b57b2c53b/achievements/showwall
 * Created by dllo on 16/1/19.
 */
public class PeopleRecordFragment extends BaseFragment {
    private Context context;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private PeopleData peopleData;
    private TextView totalDuration, totalTraining, totalTrainingDay, currentCombo;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_people_record, container, false);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        totalDuration = (TextView) view.findViewById(R.id.totalDuration);
        totalTraining = (TextView) view.findViewById(R.id.totalTraining);
        totalTrainingDay = (TextView) view.findViewById(R.id.totalTrainingDay);
        currentCombo = (TextView) view.findViewById(R.id.currentCombo);
    }

//    @Subscribe
//    void onEvent(PeopleData peopleData) {
////默认调用此 方法
//        totalDuration.setText(peopleData.getData().getStatistics().getTotalDuration() + "");
//        totalTrainingDay.setText(peopleData.getData().getStatistics().getTotalTrainingDay() + "");
//        totalTraining.setText(peopleData.getData().getStatistics().getTotalTraining() + "");
//        currentCombo.setText(peopleData.getData().getStatistics().getCurrentCombo() + "");
//
//    }

    @Override
    protected void init() {
//        EventBus.getDefault().register(context);
        String recordUrl = getArguments().getString("url") + "/achievements/showwall";
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
        GsonRequest<PeopleRecordFragmentData> recordGsonRequest = new GsonRequest<PeopleRecordFragmentData>(recordUrl, PeopleRecordFragmentData.class, new Response.Listener<PeopleRecordFragmentData>() {
            @Override
            public void onResponse(PeopleRecordFragmentData response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> recordHeader = new HashMap<>();
                recordHeader.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzE3NTAzMCwiZXhwIjoxNDU1NzY3MDMwLCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.mlN1pgmF9Dt5NPlq_YqoNvXSObvsCgHcYU-9PssqzDI");
                recordHeader.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                recordHeader.put("Host", "api.gotokeep.com");
                recordHeader.put("If-None-Match", "W/\"12ca-Vbanfg658HNRQyTnNRJ/1Q\"");
                return super.getHeaders();
            }
        };
    }
}
